// DlgAddLDAPUser.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgAddLDAPUser.h"


// CDlgAddLDAPUser dialog

IMPLEMENT_DYNAMIC(CDlgAddLDAPUser, CDialog)

CDlgAddLDAPUser::CDlgAddLDAPUser(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgAddLDAPUser::IDD, pParent)
	, m_szUsers(_T(""))
{
	m_nUserID = -1;
}

CDlgAddLDAPUser::~CDlgAddLDAPUser()
{
}

void CDlgAddLDAPUser::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_USERS, m_szUsers);
}


BEGIN_MESSAGE_MAP(CDlgAddLDAPUser, CDialog)
END_MESSAGE_MAP()


// CDlgAddLDAPUser message handlers

BOOL CDlgAddLDAPUser::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgAddLDAPUser::OnOK()
{
	// update data
	UpdateData();

	// check for user
	if( m_szUsers.IsEmpty() )
	{
		AfxMessageBox( _T("Please enter user LAD ID or email address.") );
		return;
	}
	// get info
	LDAP_USER_INFO UserInfo;
	if( !appConfig.GetLDAPUser( m_szUsers, &UserInfo ))
	{
		AfxMessageBox( _T("User not found, please enter another LAN ID or email address.") );
		return;
	}

	// found user
	CString szUserInfo;
	szUserInfo.Format( _T("Add %s to database?"), UserInfo.szFullName );
	if( AfxMessageBox( szUserInfo, MB_YESNO ) == IDNO )
		return;

	// add user to DB
	LONGLONG nID = -1;
	appConfig.m_SQL.SetUserSet( &UserInfo, TRUE, &nID );

	m_nUserID = (LONG)nID;
	m_szEmail = UserInfo.szEmail;
	m_szName = UserInfo.szName;
	m_szCompany = UserInfo.szAddress;

	CDialog::OnOK();
}

void CDlgAddLDAPUser::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}
